package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.fragment.BaseFragment;
import com.czl.module_base.fragment.BaseFragment1;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_service.event.SortBeanEvent;
import com.czl.module_storehouse.activity.baseInfo.GoodsBaseInfoActivity;
import com.czl.module_storehouse.adapter.StockListChildAdapter;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockDefaultFragment extends BaseFragment1<LayoutRecyclerViewBinding> implements SimpleView {
    @InjectPresenter
    StockPresenter mStockPresenter;
    private StockListChildAdapter mAdapter;

    @Override
    protected LayoutRecyclerViewBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return LayoutRecyclerViewBinding.inflate(layoutInflater, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mAdapter = new StockListChildAdapter(new ArrayList<>());

        binding.recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Object item = adapter.getItem(position);
            if (item instanceof SortBean) {
                SortBean sortBean = (SortBean) item;
                if (((SortBean) item).getSortId() == null) {
                    return;
                }
                goActivity(sortBean);
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            SortBean item = mAdapter.getItem(position);
            goActivity(item);
        });
    }

    @Override
    protected void reload() {
        super.reload();
        if (mStockPresenter != null) {
            mStockPresenter.getStorehouseLocatStockInfo(null);
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        showLoadSuccess();
        ListBean<LocatListBean> list = (ListBean<LocatListBean>) t;

        if (list == null || list.getList() == null || list.getList().isEmpty()) {
            setEmptyView(mAdapter);
        } else {
            mAdapter.addAll(list.getList());
        }
    }

    protected void setEmptyView(BaseQuickAdapter<?, ?> adapter) {
        adapter.removeEmptyView();
        View emptyView = View.inflate(requireContext(), com.czl.module_base.R.layout.common_empty_layout, null);
        emptyView.findViewById(com.czl.module_base.R.id.ll_empty).setOnClickListener(v -> reload());
        adapter.setEmptyView(emptyView);
    }

    private void goActivity(SortBean sortBean) {
        LiveEventBus.get(SortBeanEvent.class).postOrderly(new SortBeanEvent(sortBean));
        startActivity(new Intent(getContext(), GoodsBaseInfoActivity.class));
    }
}
